class Wiretap
  attr_accessor :called, :arguments, :return_value

  def initialize(obj, method_name, params={})
    self.reset

    @pre_method = params[:pre_method]
    @real_method = params[:method] || obj.method(method_name)
    @post_method = params[:post_method]

    wt_obj = self
    obj.send(:define_singleton_method, method_name) do |*arguments|
      wt_obj.inspector(arguments)
    end
  end
	
  def inspector(args)
    @called += 1
    @arguments.push(args)
    @pre_method.call if @pre_method
    rv = @real_method.call(*args)
    @return_value.push(rv)
    @post_method.call if @post_method
    rv
  end

  def reset
    @called = 0
    @arguments = []
    @return_value = []
  end

end